package com.walle.ak47.commons.redisson;

import java.util.concurrent.CountDownLatch;

import org.redisson.Config;
import org.redisson.Redisson;
import org.redisson.RedissonClient;
import org.redisson.core.RAtomicLong;

public class RedissonPerformanceTest2 {
	private static int TOTAL_PRE_THREADS = 100;
    private static long SECONDS = 10;
    protected static boolean isSkipLock = true;

    public static void main(String[] args) {
        Config config = new Config();
        // for single server
        config.useSingleServer()
        .setAddress("127.0.0.1:6379")
        .setConnectionPoolSize(100);
        final RedissonClient redissonCli = Redisson.create(config);

        final CountDownLatch startLatch = new CountDownLatch(1);
        final CountDownLatch endLatch = new CountDownLatch(TOTAL_PRE_THREADS);
        Thread[]  threads = new Thread[TOTAL_PRE_THREADS];
        
        final RAtomicLong addLong = redissonCli.getAtomicLong("test_performance");
        addLong.set(0);

        for (int i = 0; i < TOTAL_PRE_THREADS; i++) {
            PerformanceThread runningThread = new PerformanceThread(startLatch,endLatch, addLong);
            runningThread.start();
            threads[i]=runningThread;
        }

        //开始执行
        startLatch.countDown();
        try {
            Thread.sleep(SECONDS*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < threads.length; i++) {
            PerformanceThread runningThread =(PerformanceThread)threads[i];
            runningThread.stopThrad();
        }

        try {
            endLatch.await();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        printCount(redissonCli);
        redissonCli.shutdown();
    }


    public static class  PerformanceThread extends Thread{
        private volatile boolean isRunning = true;
        private CountDownLatch startLatch ;
        private CountDownLatch endLatch;
        private RAtomicLong addLong;
        private PerformanceThread(CountDownLatch startLatch ,CountDownLatch endLatch, RAtomicLong addLong){
            this.startLatch = startLatch;
            this.endLatch = endLatch;
            this.addLong =addLong;
        }

        @Override
        public void run() {
            try {
                startLatch.await();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }

            while (isRunning) {
                try {
                	
                    addLong.addAndGet(1);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                }
            }

            endLatch.countDown();
        }

        public void stopThrad(){
            isRunning = false;
        }
    }

    /**
     * 最后获得总数
     * @param redissonCli
     */
    public static void printCount(RedissonClient redissonCli){
        RAtomicLong addLong = redissonCli.getAtomicLong("test_performance");
        System.out.println("count tps is:"+(addLong.get()/SECONDS));
    }
    
}
